//
//  ViewController.m
//  share
//
//  Created by lanou3g on 15/7/21.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "shareViewController.h"
#import "masegeTableViewCell.h"
#import "Request.h"
#import "shareModel.h"
#import "JSONKit.h"
#import "MJRefresh.h"
@interface shareViewController ()<UITableViewDelegate,UITableViewDataSource,RequestDataDelegate>
@property(nonatomic,retain)UITableView *allTableView;
@property(nonatomic,retain)NSMutableArray *masegeArr;
@property(nonatomic,retain)UISegmentedControl *segment;
@property(nonatomic,assign)NSInteger seIndex;

@end

@implementation shareViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"妈妈圈";
    
    //创建一个segmentedControl
    [self layoutSegmentedControl];
    //创建一个tableview添加在视图上
    [self layoutTableView];
    //初始化数组
    self.masegeArr = [NSMutableArray array];
    
    //初始界面时也可以刷新界面
    [self layoutShuaXin:@"all"];
    self.navigationController.navigationBar.barTintColor =[UIColor colorWithRed:255 / 256.0 green:204 / 256.0 blue:204 / 256.0 alpha:1];
}

#pragma mark - 布局 -
//布局segment
-(void)layoutSegmentedControl
{
    //1创建对象
    self.segment = [[UISegmentedControl alloc]initWithItems:@[@"全部",@"热门"] ];
    //2配置属性
    _segment.frame = CGRectMake(0,64,self.view.frame.size.width,35);
    //(1)设置选种颜色
    _segment.tintColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:1];
    
    //(2)设置选中的分段
    _segment.selectedSegmentIndex  = 0;
    //(3)修改分段对应的列表
    [_segment setTitle:@"全部" forSegmentAtIndex:0];
    [_segment setTitle:@"热门" forSegmentAtIndex:1];
    //设置图片
    //    [_segment setImage:[[UIImage imageNamed:@"all3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];
    //    [_segment setImage:[[UIImage imageNamed:@"hot2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:3];
    //请求数据
    [self requestDataWithAF:@"all"];
    //添加事件
    [_segment addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    //3添加父视图
    [self.view addSubview:_segment];
}
//布局tableview
-(void)layoutTableView{
    self.allTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 101, self.view.frame.size.width, self.view.frame.size.height-50) style:UITableViewStylePlain];
    self.allTableView.backgroundColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:1];;
    self.allTableView.delegate = self;
    self.allTableView.dataSource = self;
    [self.view addSubview:_allTableView];
    
}

#pragma mark - 数据请求 -
//请求数据
-(void)requestDataWithAF:(NSString *)str{
    Request *request = [[Request alloc]init];
    request.delegate = self;
    NSString *urlStr = [NSString stringWithFormat:@"http://api.addinghome.com/lightforum/v2/timeline_%@?start=0&size=50&oauth_token=",str];
    [request requestWithUrl:urlStr];
}
//请求协议方法
-(void)requestSucess:(NSData *)data{
    [self.masegeArr removeAllObjects];
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    
    //封装model
    for (NSDictionary *dic in arr) {
        shareModel *sharmodel = [[shareModel alloc]init];
        sharmodel.imageArr = [[NSArray alloc]init];
        sharmodel.idStr = [dic valueForKey:@"postId"];
        NSMutableArray *imageAr = [dic valueForKey:@"imageUrl"];
        NSString *imageArrString = [imageAr JSONString];
        //删掉反斜
        NSString *str =[imageArrString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
        //***************
        sharmodel.imageArr = [str objectFromJSONString];
        
        NSLog(@"%@",sharmodel.imageArr);
        
        sharmodel.bodyStr = [dic valueForKey:@"body"];
        sharmodel.cityStr = [dic valueForKey:@"cityName"];
        [self.masegeArr addObject:sharmodel];
    }
    [self.allTableView reloadData];
}
-(void)requestError:(NSError *)error{
    
}

#pragma mark - 刷新 -
//布局上拉刷新下拉刷新
-(void)layoutShuaXin:(NSString *)str{
    Request *request = [[Request alloc]init];
    request.delegate = self;
    //下拉刷新
    self.allTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        NSString *urlStr = [NSString stringWithFormat:@"http://api.addinghome.com/lightforum/v2/timeline_%@?start=0&size=50&oauth_token=",str];
        [request requestWithUrl:urlStr];
        [self.allTableView.header endRefreshing];
    }];
    
    //上拉刷新
    __block int i = 0;
    self.allTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        ++i;
        if (i>=8) {
            NSLog(@"数据已经全部加载完");
            return ;
        }
        NSString *idstring = [[self.masegeArr firstObject] valueForKey:@"idStr"];
        NSString *urlStr = [NSString stringWithFormat:@"http://api.addinghome.com/lightforum/v2/timeline_%@?size=%d&postId=%@&oauth_token=&start=0",str,i*50,idstring];
        [request requestWithUrl:urlStr];
        [self.allTableView.footer endRefreshing];
    }];
}

#pragma mark - segmented点击事件 -
//segmented点击事件
-(void)segmentedAction:(UISegmentedControl *)se{
    self.seIndex = se.selectedSegmentIndex;
    if (se.selectedSegmentIndex == 0) {
        //请求数据
        [self requestDataWithAF:@"all"];
        [self layoutShuaXin:@"all"];

    }
    if (se.selectedSegmentIndex == 1) {
        //删除原来cell
        [self.masegeArr removeAllObjects];
        [self.allTableView reloadData];
        [self requestDataWithAF:@"hot"];
        [self layoutShuaXin:@"hot"];
        
    }
    
}

#pragma mark - UITableView的协议方法 -
//协议方法
//分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//分行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"%ld",self.masegeArr.count);
    return self.masegeArr.count;
    
}
//cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    masegeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[masegeTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    cell.backgroundColor = [UIColor colorWithRed:255/256.0 green:204/256.0 blue:204/256.0 alpha:1];
    shareModel *model =self.masegeArr[indexPath.row];
    cell.sharM = model;
    if (cell.imageViewArr.count != 0) {
        UIImageView *iv = [cell.imageViewArr objectAtIndex:0];
        iv.image = [UIImage imageNamed:@"123.png"];
    }
    return cell;
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    shareModel *model =self.masegeArr[indexPath.row];
   
    if (model.imageArr.count==0) {
        return [masegeTableViewCell stringHeight:model.bodyStr] + 40;
    }
    return 165+[masegeTableViewCell stringHeight:model.bodyStr] + 40;
}


















@end
